package com.atxiao.demo.controller;

import com.atxiao.demo.bean.Account;
import com.atxiao.demo.bean.Result;
import com.atxiao.demo.bean.User;
import com.atxiao.demo.service.AccountService;
import com.atxiao.demo.service.SaveService;
import com.atxiao.demo.service.TransferService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TransferController {
    @Resource
    private TransferService service;

    @Resource
    private AccountService accountService;

    @Resource
    private SaveService saveService;

    @PostMapping("/transfer1")
    public String IntoPark(HttpServletRequest req, HttpSession session,
                           String cardNoA, String cardNoB, Double money1,Double money2, Model model) {
        Result flag = service.transfer(req);
        //查询session是否存在user,即登陆
        Object user_object = session.getAttribute("user");
        User user = (User) user_object;
        List<Account> accountsByUser = accountService.getAccountsByUser(user.getUserIdCard());
        List<String> accountNos = new ArrayList<>();
        for (Account account : accountsByUser) {
            accountNos.add(account.getCardNum());
        }
        model.addAttribute("msg", "进入转账界面");
        model.addAttribute("oNos", accountNos);


        //判断是否选择卡号
        if (Strings.isBlank(cardNoA) || Strings.isBlank(cardNoB)) {
            //未选卡号
            String msg = "未选卡号,不能进行存款操作";
            model.addAttribute("msg", msg);
            //返回页面
            return "transfer";
        }

        // 判断该卡号是否为本人卡号，防止注入数据
        int i = 0;
        for (int i1 = 0; i1 < accountsByUser.size(); i1++) {
            if (accountsByUser.get(i1).getCardNum().equals(cardNoA)) {
                i = 1;
            }
        }
        if (i == 0) {
            //非本人卡号
            String msg = "卡号错误";
            model.addAttribute("msg", msg);
            //返回页面
            return "transfer";
        }

        //判断输入的两次金额(金额及确认金额是否相同)
        if (money1 == null || money2 == null || money1.compareTo(money2) != 0 || money1 <= 0.00) {
            String msg = "请查看您两次输入的金额是否相同";
            //两次输入不相同
            model.addAttribute("msg", msg);
            //返回页面
            return "transfer";
        }

        //存款操作
        Result save = saveService.transfer( cardNoB,cardNoA, money1);
        if (save.getCode().equals(Result.errorResult().getCode())) {
            //具体错误信息
            String msg = save.getMessage();
            model.addAttribute("msg", msg);
            //返回页面
            return "transfer";
        }

        //返回首页]

        return "redirect:/main";
    }

    //去取款页面
    @GetMapping("/transfer")
    public String toTransfer(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<Account> accountsByUser = accountService.getAccountsByUser(user.getUserIdCard());
        List<String> accountNos = new ArrayList<>();
        for (Account account : accountsByUser) {
            accountNos.add(account.getCardNum());
        }
        model.addAttribute("msg", "进入转账界面");
        model.addAttribute("oNos", accountNos);

        return "transfer";
    }
}
